// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property,executeInEditMode} = cc._decorator;



import Card_Alpha from "../../common/script/Card_Alpha";
import PlayerInfo from "../../common/script/PlayerInfo";
import TipsShell from "../../common/script/TipsShell";
import CardCtrl from "./CardCtrl";
import ns from "./EgyptianAdventureNS";
@ccclass
//@executeInEditMode
export default class EgyptianAdventure extends cc.Component {

    @property(cc.Prefab)
    ruleLayer: cc.Prefab = null;

    @property(cc.Node)
    cardRoot: cc.Node = null;
    @property(cc.Node)
    startBtns: cc.Node = null;
    @property(cc.Node)
    gameOverPanel: cc.Node = null;
    @property(cc.Node)
    touchMask: cc.Node = null;
    @property(cc.Label)
    totalWinLab: cc.Label = null;

    @property(cc.Prefab)
    tipsPrefab: cc.Prefab = null;


    @property(cc.Prefab)
    ShellCard: cc.Prefab = null;

    @property([cc.SpriteFrame])
    cardSp: cc.SpriteFrame[] = [];

    @property([cc.SpriteFrame])
    rewardSp: cc.SpriteFrame[] = [];
    
    gameTypeCost  = {
        easy: 100,
        normal: 1000,
        hard: 10000
    } 

    gameCost: number = 0;
    rewardRate: number = 0.04;

    totalWin: number = 0;
    restCardCount: number = 25;
    shouldTrigger(chance: number): boolean {
        // 生成一个 0 到 100 之间的随机数
        const random = Math.random();
        // 比较随机数与触发几率，返回比较结果
        return random <= chance;
    }

    start () {
        //this.startGame("normal");
        this.cardRoot.removeAllChildren()
    }

    showTouchMask(){
        this.touchMask.active = true;

    }

    checkGameOver(){
        if(this.restCardCount == 0){
            this.showGameOverPanel();
        }
    }

    showGameOverPanel() {
        this.gameOverPanel.active = true;
        this.totalWinLab.string = (this.totalWin).toFixed(2);
        this.touchMask.active = false;
    }
    hideGameOverPanel() {
        this.startBtns.active = true;
        this.gameOverPanel.active = false;
        this.cardRoot.removeAllChildren();
    }

    startGame(arg) {
        this.totalWin = 0;
        this.restCardCount = 25;
        this.startBtns.active = false;
        this.cardRoot.removeAllChildren();
        for(let i = 0 ; i < 25 ; i++){
            let reward: ns.Reward = null;
            this.cardRoot.addChild(cc.instantiate(this.ShellCard));
            let card = this.cardRoot.children[i].getComponent(Card_Alpha);

            if(this.shouldTrigger(0.05)){
                reward = {type: ns.RewardType.mummy, value: 0}
                card.setCardFront(this.rewardSp[1]);
                card.rewardCoinLab.string = "";
            }else{
                let value = (this.gameCost * (this.rewardRate + Math.random() * this.rewardRate)).toFixed(2)
                reward = {type: ns.RewardType.coin, value: parseFloat(value)}
                card.setCardFront(this.rewardSp[0]);
                card.rewardCoinLab.string = (reward.value).toFixed(2);
            }
            let spIndex = Math.floor(Math.random() * this.cardSp.length);
            let cardCtrl = this.cardRoot.children[i].getComponent("CardCtrl");
            cardCtrl.init(i, reward,this);
            card.setCardBack(this.cardSp[spIndex]);
        }
        
    }

    public clickExit(event: cc.Event.EventTouch, data: any) {
        this.scheduleOnce(() => {
            cc.director.loadScene('lobby');
        }, 0)
    }

    public showRuleLayer() {
        let ruleLayer = cc.instantiate(this.ruleLayer);
        this.node.addChild(ruleLayer);
        ruleLayer.setPosition(0, 0);
    }
    nodePool: cc.NodePool = new cc.NodePool();
    showTip(arg) {
        let tip = cc.instantiate(this.tipsPrefab);
        tip.getComponent(TipsShell).init(this.nodePool);
        this.node.addChild(tip);
        tip.getComponent(TipsShell).showTips(arg);
    }

    onStartBtnClick(event,arg) {
        



        
        switch(arg){
            case "easy":
                this.gameCost = 100;
                if(this.gameCost > PlayerInfo.ins.getMoney()){
                    this.showTip("Not enough money");
                }else{
                    PlayerInfo.ins.subMoney(this.gameCost);
                    this.startGame("easy");
                }
            break;
            case "normal":
                this.gameCost = 1000;
                if(this.gameCost > PlayerInfo.ins.getMoney()){
                    this.showTip("Not enough money");
                }else{
                    PlayerInfo.ins.subMoney(this.gameCost);
                    this.startGame("normal");
                }
                
            break;
            case "hard":
                this.gameCost = 10000;
                if(this.gameCost > PlayerInfo.ins.getMoney()){
                    this.showTip("Not enough money");
                }else{
                    PlayerInfo.ins.subMoney(this.gameCost);
                    this.startGame("hard");
                }
                
            break;
            case "openAll":
                this.cardRoot.children.forEach((item,i) => {
                    let card = item.getComponent(CardCtrl);
                    this.scheduleOnce(() => {
                        card.onClick();
                    }, 0.1 * i);
                    
                })
            break;

            
        }

        
    }



    // update (dt) {}
}
